﻿@page "/So/SaleReturnDetail/Index"
@using nERP.ViewModel.So.SaleReturnDetailVMs
@using System.ComponentModel.DataAnnotations
@inherits BasePage
@attribute [ActionDescription("_Page.So.SaleReturnDetail.Index", "nERP.So.Controllers,SaleReturnDetail")]

<Table @ref="SaleReturnDetailListVMdataTable" TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View" OnQueryAsync="SaleReturnDetailListVMOnSearchSaleReturnDetail"
       IsStriped="true" IsBordered="true" ShowRefresh="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
       ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
       AllowResizing="true" ShowDefaultButtons="false" IsPagination="true" AllowDragColumn="true" ShowColumnList="true" ShowSearch="true" SearchTooltip=""
       OnResetSearchAsync="OnResetSearchAsync" SearchMode="SearchMode.Top" CollapsedTopSearch="true" ShowSearchText="false">
    <SearchTemplate>
        <ValidateForm Model="@Model">
            <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
                <Select @bind-Value="@Model.Searcher.SaleReturnId" Items="AllSaleReturns" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <BootstrapInput @bind-Value="@Model.Searcher.SaleReturnDetailCode" />
                <Select @bind-Value="@Model.Searcher.SaleOutWarehouseDetailId" Items="AllSaleOutWarehouseDetails" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <Select @bind-Value="@Model.Searcher.SaleId" Items="AllSales" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <Select @bind-Value="@Model.Searcher.SaleDetailId" Items="AllSaleDetails" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <Select @bind-Value="@Model.Searcher.GoodsId" Items="AllGoodss" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <WTDateRange @bind-Value="@Model.Searcher.CreateTime" />
                <BootstrapInput @bind-Value="@Model.Searcher.CreateBy" />
            </Row>
        </ValidateForm>
    </SearchTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.SaleReturnDetail_Seq" Text="@WtmBlazor.Localizer["Page.序号"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_SaleReturn" Text="@WtmBlazor.Localizer["Page.销售退货"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_SaleReturnDetailCode" Text="@WtmBlazor.Localizer["Page.退货明细单号"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_SaleOutWarehouseDetail" Text="@WtmBlazor.Localizer["Page.销售出库明细"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_Sale" Text="@WtmBlazor.Localizer["Page.销售订单"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_SaleDetail" Text="@WtmBlazor.Localizer["Page.销售明细"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_Goods" Text="@WtmBlazor.Localizer["Page.商品"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_GoodsName" Text="@WtmBlazor.Localizer["Page.商品名称"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_GoodsSpec" Text="@WtmBlazor.Localizer["Page.商品规格"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_GoodsUnit" Text="@WtmBlazor.Localizer["Page.计量单位"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_ReturnNumber" Text="@WtmBlazor.Localizer["Page.退货数量"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_UnitPrice" Text="@WtmBlazor.Localizer["Page.退货单价"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_TotalPrice" Text="@WtmBlazor.Localizer["Page.退货金额"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_DiscountPrice" Text="@WtmBlazor.Localizer["Page.折扣金额"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_TaxRate" Text="@WtmBlazor.Localizer["Page.税率"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_TaxUnitPrice" Text="@WtmBlazor.Localizer["Page.含税单价"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_TaxPrice" Text="@WtmBlazor.Localizer["Page.税额"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_TotalTaxPrice" Text="@WtmBlazor.Localizer["Page.含税金额"]" FormatString="0.##" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
        <TableColumn @bind-Field="@context.SaleReturnDetail_UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
    </TableColumns>

    <RowButtonTemplate>
        @if (IsAccessable("/api/So/SaleReturnDetail/Edit") && context.SaleReturnDetail_Status != nERP.Model.dStatusTypeEnum.Reviewed)  //已审批不可见
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-pencil-square" Color="Color.Warning" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="()=>OnEditClick(context)" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/{id}"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-info-circle" Color="Color.Info" Text="@WtmBlazor.Localizer["Page.详情"]" OnClick="()=>OnDetailsClick(context)" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/BatchDelete") && context.SaleReturnDetail_Status != nERP.Model.dStatusTypeEnum.Reviewed)  //已审批不可见
        {
            <PopConfirmButton OnConfirm="() => OnDeleteClick(context)" Size="Size.ExtraSmall" Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]"
                              ConfirmButtonColor="Color.Danger" />
        }
    </RowButtonTemplate>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/So/SaleReturnDetail/Create"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View" Icon="fa fa-plus" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="(x)=>OnCreateClick()" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/BatchDelete"))
        {
            <TableToolbarPopConfirmButton TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View"
                                          Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/BatchEdit"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.BatchEdit"]" OnClickCallback="(x)=>OnBatchEditClick()" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/Import"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View" Icon="fa fa-tasks" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="(x)=>OnImportClick()" />
        }
        @if (IsAccessable("/api/So/SaleReturnDetail/SaleReturnDetailExportExcel"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View" Icon="fa fa-arrow-circle-down" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="(x)=>OnExportClick()" IsAsync="true" />
        }
    </TableToolbarTemplate>

</Table>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private SaleReturnDetailListVM Model = new SaleReturnDetailListVM();
    private Table<nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View> SaleReturnDetailListVMdataTable;
    private Task OnResetSearchAsync(SaleReturnDetail_View item)
    {
        Model.Searcher = new SaleReturnDetailSearcher();
        item = new SaleReturnDetail_View();
        return Task.CompletedTask;
    }
    private async Task SaleReturnDetailListVMDoSearch()
    {
        await SaleReturnDetailListVMdataTable.QueryAsync();
    }
    private async Task<QueryData<nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View>> SaleReturnDetailListVMOnSearchSaleReturnDetail(QueryPageOptions opts)
    {

        return await StartSearch<nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View>("/api/So/SaleReturnDetail/SearchSaleReturnDetail", Model.Searcher, opts);
    }
    private async Task OnCreateClick()
    {
        var id = SaleReturnDetailListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Create>(@WtmBlazor.Localizer["Sys.Create"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await SaleReturnDetailListVMdataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View item)
    {
        if (await OpenDialog<Edit>(@WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await SaleReturnDetailListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View item)
    {
        if (await OpenDialog<Details>(@WtmBlazor.Localizer["Page.详情"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await SaleReturnDetailListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDeleteClick(nERP.ViewModel.So.SaleReturnDetailVMs.SaleReturnDetail_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/So/SaleReturnDetail/batchdelete", (s) => "Sys.OprationSuccess");
        await SaleReturnDetailListVMdataTable.QueryAsync();
    }

    private async Task OnBatchDeleteClick()
    {
        if (SaleReturnDetailListVMdataTable.SelectedRows?.Any() == true)
        {
            if (SaleReturnDetailListVMdataTable.SelectedRows?.Where(z0 => z0.SaleReturnDetail_Status == nERP.Model.dStatusTypeEnum.Reviewed).Count() > 0)
            {
                await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], "选择的项目有【已审批】单据，不可操作！");
                return;
            }
            await PostsData(SaleReturnDetailListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList(), $"/api/So/SaleReturnDetail/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await SaleReturnDetailListVMdataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnBatchEditClick()
    {
        if (SaleReturnDetailListVMdataTable.SelectedRows?.Any() == true)
        {
            if (SaleReturnDetailListVMdataTable.SelectedRows?.Where(z0 => z0.SaleReturnDetail_Status == nERP.Model.dStatusTypeEnum.Reviewed).Count() > 0)
            {
                await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], "选择的项目有【已审批】单据，不可操作！");
                return;
            }
            if (await OpenDialog<BatchEdit>(WtmBlazor.Localizer["Sys.BatchEdit"], x => x.ids == SaleReturnDetailListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToArray(), isMax: false) == DialogResult.Yes)
            {
                await SaleReturnDetailListVMdataTable.QueryAsync();
            }
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnImportClick()
    {
        var id = SaleReturnDetailListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Import>(@WtmBlazor.Localizer["Sys.Import"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await SaleReturnDetailListVMdataTable.QueryAsync();
        }
    }

    private async Task OnExportClick()
    {
        if (SaleReturnDetailListVMdataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/So/SaleReturnDetail/SaleReturnDetailExportExcelByIds", SaleReturnDetailListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/So/SaleReturnDetail/SaleReturnDetailExportExcel", Model.Searcher);
        }
    }


    private List<SelectedItem> AllSaleReturns = new List<SelectedItem>();
    private List<SelectedItem> AllSaleOutWarehouseDetails = new List<SelectedItem>();
    private List<SelectedItem> AllSales = new List<SelectedItem>();
    private List<SelectedItem> AllSaleDetails = new List<SelectedItem>();
    private List<SelectedItem> AllGoodss = new List<SelectedItem>();
    protected override async Task OnInitializedAsync()
    {


        AllSaleReturns = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturnDetail/GetSaleReturns");
        AllSaleReturns.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSaleOutWarehouseDetails = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturnDetail/GetSaleOutWarehouseDetails");
        AllSaleOutWarehouseDetails.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSales = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturnDetail/GetSales");
        AllSales.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSaleDetails = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturnDetail/GetSaleDetails");
        AllSaleDetails.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllGoodss = await WtmBlazor.Api.CallItemsApi("/api/So/SaleReturnDetail/GetGoodss");
        AllGoodss.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });

        await base.OnInitializedAsync();
    }
}
